/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package skyproc;

import java.io.IOException;
import java.util.zip.DataFormatException;
import lev.LExporter;
import lev.LStream;
import skyproc.exceptions.BadParameter;
import skyproc.exceptions.BadRecord;

/**
 *
 * @author Justin Swanson
 */
class SubFloat extends SubRecord {

    float data;

    SubFloat(Type type) {
	super(type);
    }

    @Override
    SubRecord getNew(Type type) {
	return new SubFloat(type);
    }

    @Override
    int getContentLength(Mod srcMod) {
	return 4;
    }

    @Override
    void parseData(LStream in) throws BadRecord, DataFormatException, BadParameter, IOException {
	super.parseData(in);
	data = in.extractFloat();
	if (logging()) {
	    logSync(toString(), "Setting " + toString() + " to : " + print());
	}
    }

    @Override
    public String print() {
	if (isValid()) {
	    return String.valueOf(data);
	} else {
	    return super.toString();
	}
    }

    @Override
    void export(LExporter out, Mod srcMod) throws IOException {
	if (isValid()) {
	    super.export(out, srcMod);
	    out.write(data);
	}
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null) {
            return false;
        }
        if (!(o instanceof SubFloat)) {
            return false;
        }
        SubFloat s = (SubFloat) o;
        return (Float.compare(this.data, s.data) == 0);
    }

    @Override
    public int hashCode() {
	int hash = 7;
	hash = 67 * hash + Float.floatToIntBits(this.data);
	return hash;
    }
    
    /*
     * SkyBash methods.
     */
    /**
     * Merges straight SubFloats with logging capabilities.
     *
     * @param no The new SubFloat to be merged.
     * @param bo The base SubFloat, to prevent base data from being
     * re-merged.
     * @return The modified SubFloat.
     */
    @Override
    public SubRecord merge(SubRecord no, SubRecord bo) {
        SubFloat f = this;
        if (!(no == null && bo == null && (no instanceof SubFloat) && (bo instanceof SubFloat))) {
            final SubFloat nf = (SubFloat) no;
            final SubFloat bf = (SubFloat) bo;
            if (!f.equals(nf) && !nf.equals(bf)) {
                f = nf;
                if (SkyBash.fullLogging) {
                    SkyBash.logMerge(type[0].toString(), nf.toString());
                }
            }
        }
        return f;
    }
}
